package com.itheima.controller;

import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.UploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "套餐管理开发")  // 作用在类上
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @GetMapping("setmeal/findCheckgroupInfoBySetmealId/{id}")
    public Result findCheckgroupInfoBySetmealId(@PathVariable("id") Integer id){
        return new Result(setmealService.findCheckgroupInfoBySetmealId(id));
    }


    @PostMapping("setmeal/findPage")
    @ApiOperation(value = "分页查询", notes = "根据客户端请求，完成分页查询")//   添加业务方法上
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "分页页码", required = true, paramType = "form", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录个数", required = true, paramType = "form", dataType = "Integer"),
            @ApiImplicitParam(name = "queryString", value = "查询条件", required = false, paramType = "form", dataType = "String")
    })
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        return new Result(setmealService.findPage(queryPageBean)); //   total  +  List
    }

    //完成检查项管理的添加功能
    //进行全局异常处理
    @PostMapping("setmeal/saveOrUpdate")
    //根据传入的数据是否有id来判断调用哪个方法
    public Result saveOrUpdateSetmealInfo(@RequestBody SetmealDTO setmealDTO) {
        return new Result(setmealService.saveOrUpdateSetmealInfo(setmealDTO));
    }

    @PostMapping("setmeal/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile) {
        try {
            //上传文件对象
            //1.先获取文件的原始文件名
            String originalFilename = multipartFile.getOriginalFilename();
            //2.使用工具类生成一个新的文件名
            String uuidfilename = UploadUtils.generateRandonFileName(originalFilename);

            //使用封装的阿里云工具类上传文件
            AliyunUtils.uploadMultiPartFileToAliyun(multipartFile.getBytes(), uuidfilename);

            //将图片信息保存在set集合中,并在redis中也保存一份设置了延时时间的数据
            RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET,uuidfilename);//将图片存储到set集合

            //存储一份到redis中10分钟,超过10分钟自动删除
            RedisUtil.set(RedisConstant.SINGLE_PIC+uuidfilename,uuidfilename,10, TimeUnit.MINUTES);

            //将新的文件名传递给前端,由前端的handleAvatarSuccess(res)的res获取
            return new Result(uuidfilename);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }

    }


}








